package com.xy.service.mainData;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xy.api.model.dto.mainData.VendorDTO;
import com.xy.api.model.request.mainData.VendorProductRequest;
import com.xy.api.model.request.mainData.VendorRequest;
import com.xy.api.model.response.Rows;
import com.xy.api.service.mainData.IVendorService;
import com.xy.auth.UserContext;
import com.xy.mapper.mainData.ContactMapper;
import com.xy.mapper.mainData.VendorMapper;
import com.xy.mapper.mainData.VendorProductMapper;
import com.xy.model.po.mainData.VendorPO;
import com.xy.model.po.mainData.VendorProductPO;
import com.xy.utils.CustomBeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class VendorServiceImpl implements IVendorService {

    @Autowired
    private VendorMapper vendorMapper;

    @Autowired
    private VendorProductMapper vendorProductMapper;

    @Autowired
    private ContactMapper contactMapper;

    @Override
    public Rows<VendorDTO> queryVendorList(VendorRequest request) {
        Rows<VendorDTO> result = new Rows<>();
        LambdaQueryWrapper<VendorPO> vendorWrapper = new LambdaQueryWrapper<>();
        vendorWrapper.eq(VendorPO::getIsDeleted, 0);
        if (StringUtils.hasText(request.getName())) {
            vendorWrapper.like(VendorPO::getName, "%" + request.getName() + "%");
        }
        int pageSize = null != request.getPageSize() ? request.getPageSize() : 10;
        int pageNum = null != request.getPageNum() ? request.getPageNum() : 1;
        Page<VendorPO> vendorPOPage = new Page<>(pageNum, pageSize);
        vendorPOPage = vendorMapper.selectPage(vendorPOPage, vendorWrapper);

        result.setTotal(vendorPOPage.getTotal());
        result.setRows(CustomBeanUtil.copyListProperties(vendorPOPage.getRecords(), VendorDTO::new));
        return result;
    }

    @Override
    public Rows<VendorDTO> queryAllVendorList() {
        LambdaQueryWrapper<VendorPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VendorPO::getIsDeleted, 0);
        List<VendorPO> vendorPOS = vendorMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(vendorPOS)) {
            return new Rows<>(new ArrayList<>(), 0);
        }
        List<VendorDTO> vendorList = CustomBeanUtil.copyListProperties(vendorPOS, VendorDTO::new);
        return new Rows<>(vendorList, vendorList.size());
    }

    @Override
    public VendorDTO vendorDetail(Long id) {
        Assert.notNull(id, "供应商ID必填");
        VendorDTO vendor = new VendorDTO();
        VendorPO vendorPO = vendorMapper.selectById(id);
        CustomBeanUtil.copyProperties(vendorPO, vendor);
        return vendor;
    }


    @Override
    public VendorDTO editVendor(VendorDTO request) {
        VendorPO vendor = new VendorPO();
        CustomBeanUtil.copyProperties(request, vendor);
        if (null == request.getId()) {
            addVendor(vendor);
        } else {
            updateVendor(vendor);
        }

        VendorDTO vendorDTO = new VendorDTO();
        CustomBeanUtil.copyProperties(vendor, vendorDTO);
        return vendorDTO;
    }

    private void addVendor(VendorPO vendor) {
        vendor.setCreateBy(UserContext.get().getId());
        vendorMapper.insert(vendor);
    }

    private void updateVendor(VendorPO vendor) {
        Assert.notNull(vendor.getId(), "供应商ID必填");
        vendor.setUpdateBy(UserContext.get().getId());
        vendorMapper.updateById(vendor);
    }

    @Override
    @Transactional
    public void deleteVendor(Long id) {
        Assert.notNull(id, "供应商ID必填");
        VendorPO deleteRecord = new VendorPO();
        deleteRecord.setId(id);
        deleteRecord.setUpdateBy(UserContext.get().getId());
        deleteRecord.setIsDeleted(1);
        vendorMapper.updateById(deleteRecord);

        LambdaQueryWrapper<VendorProductPO> deleteVendorProductWrapper = new LambdaQueryWrapper<>();
        deleteVendorProductWrapper.eq(VendorProductPO::getVendorId, id);
        VendorProductPO deleteVendorProductRecord = new VendorProductPO();
        deleteVendorProductRecord.setIsDeleted(1);
        vendorProductMapper.update(deleteVendorProductRecord, deleteVendorProductWrapper);
    }

    @Override
    public void deleteVendorProduct(VendorProductRequest request) {
        LambdaQueryWrapper<VendorProductPO> deleteVendorProductWrapper = new LambdaQueryWrapper<>();
        deleteVendorProductWrapper
                .eq(VendorProductPO::getVendorId, request.getVendorId())
                .eq(VendorProductPO::getProductId, request.getProductId());
        VendorProductPO deleteVendorProductRecord = new VendorProductPO();
        deleteVendorProductRecord.setIsDeleted(1);
        vendorProductMapper.update(deleteVendorProductRecord, deleteVendorProductWrapper);
    }

    @Override
    public Map<Long, VendorDTO> getVendorByIds(List<Long> vendorIds) {
        if (CollectionUtils.isEmpty(vendorIds)) {
            return new HashMap<>();
        }
        LambdaQueryWrapper<VendorPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VendorPO::getIsDeleted, 0)
                .in(VendorPO::getId, vendorIds);
        List<VendorPO> vendorPOS = vendorMapper.selectList(queryWrapper);
        List<VendorDTO> vendorList = CustomBeanUtil.copyListProperties(vendorPOS, VendorDTO::new);
        return vendorList.stream().collect(Collectors.toMap(e -> e.getId(), e -> e));
    }
}
